#!/bin/bash


## email for server side
## gitea seafile bitwarden
(
    cd "`dirname "$0"`"
    PATH="/bin:/sbin:${PATH}"
    if [ -z "$TZ" ];then
        TZ="Asia/Shanghai"
    fi

    emailconfigfile="./email.conf"
    if [ ! -f "$emailconfigfile" ];then
        echo "[Err] $0 , can't find email config file :: $emailconfigfile"
        echo "[Warn]  Attension ::  and container for 'bitwarden' will not run . "
        exit 1
    fi
    source "$emailconfigfile"

    dataMnt=/mnt/server.lan/data
    dataStorage=$dataMnt/dataStorage



    function addTOBitwarden() {
        # EMAIL_ARGS_FOR_EMAIL="-e SMTP_HOST=<smtp.domain.tld> -e SMTP_FROM=<bitwarden@domain.tld> -e SMTP_PORT=587 -e SMTP_SSL=true -e SMTP_USERNAME=<username> -e SMTP_PASSWORD=<password> "
        local EMAIL_ARGS_FOR_EMAIL=" "
        if [ ! -z "${_EMAIL_SECRET}" ];then
            EMAIL_ARGS_FOR_EMAIL=" -e SMTP_HOST=${_EMAIL_HOST} -e SMTP_FROM=${_EMAIL} -e SMTP_PORT=587 \
            -e SMTP_SSL=true -e SMTP_USERNAME=${_EMAIL} -e SMTP_PASSWORD=${_EMAIL_SECRET}  -e SMTP_EXPLICIT_TLS=false "
        fi


        # run docker container
        ## default port=ROCKET_PORT=80
        container_name="bitwarden.server.lan"
        dataPath="$dataStorage/$container_name"

        docker rm "$container_name"  &> /dev/null


        docker run -d --restart=always \
            --name="${container_name}" \
            -v "${dataPath}":/data/ \
            -e TZ=$TZ \
            -v /usr/share/zoneinfo/$TZ:/etc/localtime:ro \
            -v /etc/timezone:/etc/timezone:ro \
            -e ROCKET_PORT=8080 \
            -e DOMAIN="https://bitwarden.server.lan" \
            ${EMAIL_ARGS_FOR_EMAIL} \
            -p 9000:8080 \
            vaultwarden/server

            # bitwardenrs/server

    }



    function addTOGitea() {
        local configfile="${dataStorage}/git.lan/gitea/conf/app.ini"
        if [ ! -f "${configfile}" ];then
            echo "[Err] app config not found, please run <'install.sh'> before this"
            return 1
        fi
        echo "[INFO]:: gitea config :: ${configfile}"

        x="MAILER_TYPE    = smtp"
        if grep -F "$x" "$configfile" &> /dev/null ;then
            echo "[Warn] already added , not changed"
            return
        fi

        { 
            echo "
[mailer]
ENABLED = true
MAILER_TYPE    = smtp
IS_TLS_ENABLED = true
HOST    = ${_EMAIL_HOST}:${_EMAIL_PORT}
FROM    = \"gitServerBot\"<${_EMAIL}>
USER    = ${_EMAIL}
PASSWD  = ${_EMAIL_SECRET}
    " 
        } >> "${configfile}"

    }



    function addTOSeafile() {
        local configfile=${dataStorage}/seafile.server.lan/conf/seahub_settings.py
        if [ ! -f "${configfile}" ];then
            echo "[Err] app config not found, please run <'install.sh'> before this"
            return 1
        fi
        echo "[INFO]:: seafile config :: ${configfile}"        

        x="EMAIL_USE_SSL = True"
        if grep -F "$x" "$configfile" &> /dev/null ;then
            echo "[Warn] already added , not changed"
            return
        fi

        {
            echo "
EMAIL_USE_SSL = True
EMAIL_HOST = '${_EMAIL_HOST}'
EMAIL_HOST_USER = '${_EMAIL}'
EMAIL_HOST_PASSWORD = '${_EMAIL_SECRET}'
EMAIL_PORT = '${_EMAIL_PORT}'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
SERVER_EMAIL = EMAIL_HOST_USER
    "
        } >> "${configfile}"

    }


######################
    exitflag=false
    function isEmpty_and_exit {
        if [ -z "$1" ];then
            echo "[X] "
            exitflag=true
        fi
        echo "[OK] "
    }

    echo "[INFO] check mailer settings from  '''./email.conf''' :: 
        _EMAIL_HOST     : `isEmpty_and_exit "${_EMAIL_HOST}"` : ${_EMAIL_HOST}
        _EMAIL_PORT     : `isEmpty_and_exit "${_EMAIL_PORT}"` : ${_EMAIL_PORT}
        _EMAIL          : `isEmpty_and_exit "${_EMAIL}"` : "${_EMAIL}"
        _EMAIL_SECRET   : `isEmpty_and_exit "${_EMAIL_SECRET}"`
    "

    if ${exitflag};then
        exit 1
    fi

##########################

    function stop_container {
        echo "[Info] try to stop container :: $1"
        docker stop "$1" 
    }

    function start_container {
        echo "[Info] start container :: $1"
        docker start "$1" 2> /dev/null
    }

    echo ""
    echo "[INFO] error is OK {when stop container(bitwarden) and can't find container}"
    stop_container bitwarden.server.lan
    addTOBitwarden
    start_container bitwarden.server.lan

    echo ""
    stop_container git.lan
    addTOGitea 
    start_container git.lan

    echo ""
    stop_container seafile.server.lan
    addTOSeafile 
    start_container seafile.server.lan



)
